/**  
 * @Title VelocityViewExtResolver.java
 * @date 2016年3月7日 下午5:46:04
 * @Copyright (c) 2016, unibroad.com Inc. All rights reserved.
 */
package com.strugglerz.web.base;

import org.springframework.web.servlet.view.AbstractTemplateViewResolver;
import org.springframework.web.servlet.view.AbstractUrlBasedView;
import org.springframework.web.servlet.view.velocity.VelocityLayoutView;

/**
 * @Description 改造 VelocityLayoutViewResolver 实现 velocity view 视图解析, 约定 fragment/* 为
 *              VelocityViewResolver, 其它为 VelocityLayoutViewResolver
 * @Class com.unibroad.touchus.web.admin.base.VelocityViewExtResolver
 * @author suan@unibroad.com
 * @version V1.0
 */
public class VelocityViewExtResolver extends AbstractTemplateViewResolver {

    private String toolboxConfigLocation;

    private String layoutUrl;

    private String layoutKey;

    private String screenContentKey;

    /**
     * Creates a new instance of VelocityViewExtResolver.
     */
    public VelocityViewExtResolver() {
        super();
        this.setViewClass(VelocityExtView.class);
    }

    @Override
    protected AbstractUrlBasedView buildView(String viewName) throws Exception {

        VelocityExtView view = (VelocityExtView) super.buildView(viewName);

        if (this.layoutUrl != null) {
            view.setLayoutUrl(this.layoutUrl);
        }

        if (this.layoutKey != null) {
            view.setLayoutKey(this.layoutKey);
        }

        if (this.screenContentKey != null) {
            view.setScreenContentKey(this.screenContentKey);
        }

        view.setToolboxConfigLocation(this.toolboxConfigLocation);

        return view;

    }

    /**
     * @param toolboxConfigLocation
     *            to set - {参数含义描述}.
     */
    public void setToolboxConfigLocation(String toolboxConfigLocation) {
        this.toolboxConfigLocation = toolboxConfigLocation;
    }

    /**
     * Set the layout template to use. Default is "layout.vm".
     * 
     * @param layoutUrl
     *            the template location (relative to the template root directory)
     * @see VelocityLayoutView#setLayoutUrl
     */
    public void setLayoutUrl(String layoutUrl) {
        this.layoutUrl = layoutUrl;
    }

    /**
     * Set the context key used to specify an alternate layout to be used instead of the default
     * layout. Screen content templates can override the layout template that they wish to be wrapped
     * with by setting this value in the template, for example:<br>
     * {@code #set($layout = "MyLayout.vm" )}
     * <p>
     * The default key is "layout", as illustrated above.
     * 
     * @param layoutKey
     *            the name of the key you wish to use in your screen content templates to override the
     *            layout template
     * @see VelocityLayoutView#setLayoutKey
     */
    public void setLayoutKey(String layoutKey) {
        this.layoutKey = layoutKey;
    }

    /**
     * Set the name of the context key that will hold the content of the screen within the layout
     * template. This key must be present in the layout template for the current screen to be
     * rendered.
     * <p>
     * Default is "screen_content": accessed in VTL as {@code $screen_content}.
     * 
     * @param screenContentKey
     *            the name of the screen content key to use
     * @see VelocityLayoutView#setScreenContentKey
     */
    public void setScreenContentKey(String screenContentKey) {
        this.screenContentKey = screenContentKey;
    }

}
